package cn.jinjimi.app.poster;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.jinjimi.app.R;
import cn.jinjimi.app.poster.bean.PosterInfo;
import cn.jinjimi.app.poster.fragment.PosterContainerFragment;
import cn.jinjimi.app.main.adapter.recycler.MyBaseRecyclerAdapter;
import cn.jinjimi.app.main.widget.FrescoImageView.FrescoImageView;

/**
 * Created by lforxeverc on 16/9/22.
 */

public class PosterAdapter extends MyBaseRecyclerAdapter<PosterInfo, PosterAdapter.ViewHolder> {
    Activity mContext;
    final int dataType;
    PosterListener mListener;
    boolean showShareCount = true;

    public void setListener(PosterListener mListener) {
        this.mListener = mListener;
    }

    public PosterAdapter(Activity ctx, int dataType) {
        mContext = ctx;
        this.dataType = dataType;
        PosterConfig.Info posterConfig = PosterConfig.getConfig();
        if (posterConfig != null) {
            showShareCount = posterConfig.showShareCount;
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        super.onCreateViewHolder(parent, viewType);
        View convertView = LayoutInflater.from(mContext).inflate(dataType == PosterContainerFragment.DataType.JINJU_POSTER ? R.layout.item_jinju : R.layout.item_poster, parent, false);
        return new ViewHolder(convertView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        super.onBindViewHolder(holder, position);
        PosterInfo info = getItem(position);
        holder.ivPoster.loadView(info.thumbnailUrl);
        if (dataType == PosterContainerFragment.DataType.INFORMATION_POSTER) {
            holder.tvTitle.setText(info.title);
            if (showShareCount) {
                if (info.shareCounts == 0 || info.shareCounts == -1) {
                    holder.ivShare.setVisibility(View.GONE);
                    holder.tvCounts.setVisibility(View.GONE);
                } else {
                    holder.ivShare.setVisibility(View.VISIBLE);
                    holder.tvCounts.setVisibility(View.VISIBLE);
                    holder.tvCounts.setText(info.shareCounts + "");
                }
            }

        }
        holder.ivPoster.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onPosterItemClick(position, getItem(position));
                }
            }
        });
    }


    @Override
    protected Animator[] getAnimators(View view) {
        return new Animator[]{ObjectAnimator.ofFloat(view, "alpha", 0f, 1f)};
    }

    public class ViewHolder extends MyBaseRecyclerAdapter.MyViewHolder {
        FrescoImageView ivPoster;
        RelativeLayout rlShareContent;
        TextView tvTitle, tvCounts;
        ImageView ivShare;

        public ViewHolder(View itemView) {
            super(itemView);
            ivPoster = (FrescoImageView) itemView.findViewById(R.id.iv_poster);

            if (dataType == PosterContainerFragment.DataType.JINJU_POSTER) {

            } else {
                rlShareContent = (RelativeLayout) itemView.findViewById(R.id.rl_share_content);
                if (dataType == PosterContainerFragment.DataType.INFORMATION_POSTER) {
                    rlShareContent.setVisibility(View.VISIBLE);
                    tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
                    tvCounts = (TextView) itemView.findViewById(R.id.tv_count);
                    ivShare = (ImageView) itemView.findViewById(R.id.iv_share);
                } else {
                    rlShareContent.setVisibility(View.GONE);
                }
            }
        }
    }

    public interface PosterListener {
        void onPosterItemClick(int position, PosterInfo info);
    }
}
